package com.dstz.demo.rest.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dstz.base.rest.BaseController;
import com.dstz.demo.core.manager.ProjectManager;
import com.dstz.demo.core.model.ProjectBudget;
import com.dstz.demo.core.model.ProjectDetails;
import com.dstz.demo.core.model.Xmprojectinfo;
import com.dstz.demo.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("project")
public class ProjectController extends BaseController<Xmprojectinfo> {

    @Autowired
    ProjectManager projectManager;

    @Override
    protected String getModelDesc() {
        return null;
    }

    //接收文件解析，然后添加到预算单详情表中
    @PostMapping("uploadOne/{id}")
    public List<ProjectDetails> uploadTwo(@PathVariable("id") String id, @RequestParam("name") MultipartFile multipartFile) throws IOException {
        List<String[]> strings = POIUtils.readExcel(multipartFile);
        List<ProjectDetails> lists = new ArrayList<ProjectDetails>();
        if (strings != null && strings.size() > 1) {
            //项目预算详细信息
            for (String[] string : strings) {
                String s = "";
                if ((string[12] == null || s.equals(string[12])) && (string[13] == null || s.equals(string[13])) && (string[1] == null || s.equals(string[1])) && (string[2] == null || s.equals(string[2])) && (string[3] == null || s.equals(string[3])) && (string[4] == null || s.equals(string[4])) && (string[5] == null || s.equals(string[5])) && (string[6] == null || s.equals(string[6])) && (string[7] == null || s.equals(string[7])) && (string[8] == null || s.equals(string[8])) && (string[9] == null || s.equals(string[9])) && (string[10] == null || s.equals(string[10])) && (string[11] == null || s.equals(string[11])) && (string[14] == null || s.equals(string[14])) && (string[15] == null || s.equals(string[15])) && (string[0] == null || s.equals(string[0]))) {
                    continue;
                }
                if ("小计".equals(string[1]) || string[1] == null) {
                    continue;
                }
                //把数组中的数据拿出来，放到对象中
                ProjectDetails orderSetting = new ProjectDetails(string[12], string[13], string[1], string[2], string[3], string[4], string[5], string[6], string[7], string[8], string[9], string[10], string[11], string[14], string[15], string[0]);
                orderSetting.setProjectID(id);
                orderSetting.setStatus("0");
                lists.add(orderSetting);
            }
        }
        return projectManager.uploadTwo(lists);
    }

    //初始化预算单
    @RequestMapping("createProjectdubget")
    public Integer createProjectdubget() {
        return projectManager.createProjectdubget();
    }

    //在添加页面中点击提交的按钮，需要传过来预算单的信息和预算单详情的id
    @PostMapping("sumbit/{id}")  //这个ID是初始化的id
    public void sumbit(@PathVariable("id") String id, HttpServletRequest request) {
        String projectbudget = request.getParameter("projectbudget");
        //预算单信息
        ProjectBudget projectBudget = JSONObject.parseObject(projectbudget, ProjectBudget.class);
        projectManager.submit(id, projectBudget);
    }

    //根据预算id,查询预算单详情的信息
    @RequestMapping("projectDetails/{id}")
    public List<ProjectDetails> projectDetails(@PathVariable("id") String id) {
        return projectManager.projectDetails(id);
    }

    //查询单个项目信息
    @RequestMapping("selectOne/{id}")
    public Map selectOne(@PathVariable("id") String id) {
        return projectManager.selectOne(id);
    }

    //审核通过接口 id   是预算单id
    @PostMapping("updateStatus")
    public String updateStatus(HttpServletRequest request) {
        String list = request.getParameter("list");
        String replace = list.replace("[", "").replace("]", "");
        // 切割出来的id是
        String[] split = replace.split(",");
        projectManager.updateStatus(split);
        return "审核通过";
    }

    //审核不通过接口
    @PostMapping("updateStatusNO")
    public String updateStatusNO(String note, HttpServletRequest request) {
        String list = request.getParameter("list");
        String replace = list.replace("[", "").replace("]", "");
        // 切割出来的id是
        String[] split = replace.split(",");
        projectManager.updateStatusNO(note, split);
        return "审核不通过";
    }

    //对比表格里面的内容
    @PostMapping("appendReduce/{id}")
    public Map<String, List> appendReduce(@PathVariable("id") String id, @RequestParam("name") MultipartFile multipartFile) throws IOException {
        List<String[]> strings = POIUtils.readExcel(multipartFile);
        List<ProjectDetails> lists = new ArrayList<ProjectDetails>();
        if (strings != null && strings.size() > 1) {
            //项目预算详细信息
            for (String[] string : strings) {
                String s = "";
                if ((string[12] == null || s.equals(string[12])) && (string[13] == null || s.equals(string[13])) && (string[1] == null || s.equals(string[1])) && (string[2] == null || s.equals(string[2])) && (string[3] == null || s.equals(string[3])) && (string[4] == null || s.equals(string[4])) && (string[5] == null || s.equals(string[5])) && (string[6] == null || s.equals(string[6])) && (string[7] == null || s.equals(string[7])) && (string[8] == null || s.equals(string[8])) && (string[9] == null || s.equals(string[9])) && (string[10] == null || s.equals(string[10])) && (string[11] == null || s.equals(string[11])) && (string[14] == null || s.equals(string[14])) && (string[15] == null || s.equals(string[15])) && (string[0] == null || s.equals(string[0]))) {
                    continue;
                }
                if ("小计".equals(string[1]) || string[1] == null) {
                    continue;
                }
                //把数组中的数据拿出来，放到对象中
                ProjectDetails orderSetting = new ProjectDetails(string[12], string[13], string[1], string[2], string[3], string[4], string[5], string[6], string[7], string[8], string[9], string[10], string[11], string[14], string[15], string[0]);
                orderSetting.setProjectID(id);
                orderSetting.setStatus("0");
                lists.add(orderSetting);
            }
        }
        return projectManager.appendReduce(id, lists);
    }

    //追加减提交
    @PostMapping("sumbitAppendReduce")
    public void sumbitAppendReduce(HttpServletRequest request) {
        String list = request.getParameter("list");
        //把传过来的值装换成map集合
        List<Map> maps = JSON.parseArray(list, Map.class);
        projectManager.sumbitAppendReduce(maps);
    }

    //查询单个预算单的所有的预算单详情
    @RequestMapping("selectAll/{id}")
    public List<ProjectDetails> selectAll(@PathVariable("id") String id) {
        return projectManager.selectAll(id);
    }

    //修改预算单的版本号
    @RequestMapping("updateVersion/{id}/{version}")
    public void updateVersion(@PathVariable("id") String id, @PathVariable("version") String version) {
        projectManager.updateVersion(id, version);
    }

}
